package org.tyut.xyl.game;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.liugang.tugofwar.R;
import com.google.gson.Gson;

import org.tyut.xyl.game.util.GsonUtil;
import org.tyut.xyl.game.util.Information;
import org.tyut.xyl.game.util.OkHttpUtil;

import java.io.IOException;
import java.util.Map;
import java.util.Objects;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.Request;
import okhttp3.Response;

public class LoginActivity extends AppCompatActivity  {

    public TextView tip;

    public void onClick(View v) {
        EditText userid_et = (EditText)findViewById(R.id.login_userid);
        EditText passwd_et = (EditText)findViewById(R.id.login_passwd);
        String username = userid_et.getText().toString();
        String password = passwd_et.getText().toString();
        FormBody formBody=new FormBody.Builder()
                .add("username",username)
                .add("password",password)
                .build();
        Request request=new Request.Builder().url(Information.URL+"login")
                .post(formBody).build();
        Call call= OkHttpUtil.getOkHttpClient().newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                tip.setText("登录失败");
            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                String responseJson = Objects.requireNonNull(response.body()).string();
                Gson gson= GsonUtil.getGsonBuilder().create();
                Map<String,String> ansMap=gson.fromJson(responseJson,Map.class);
                if(ansMap.get("state")==null){
                    MainActivity.userid =ansMap.get("userid");
                    MainActivity.username=ansMap.get("username");
                    Intent intent=new Intent(LoginActivity.this,GameMainActivity.class);
                    startActivity(intent);
                    return;
                }
                if(Objects.requireNonNull(ansMap.get("state")).equals("未找到用户")){
                    tip.setText("未找到用户");
                }else if(Objects.requireNonNull(ansMap.get("state")).equals("密码错误")){
                    tip.setText("密码错误");
                }

            }
        });




    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
       // Button loginButton = (Button)findViewById(R.id.login_login_bt);
        tip = (TextView)findViewById(R.id.login_tip);
//        loginButton.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                EditText userid_et = (EditText)findViewById(R.id.login_userid);
//                EditText passwd_et = (EditText)findViewById(R.id.login_passwd);
//                String username = userid_et.getText().toString();
//                String password = passwd_et.getText().toString();
//                FormBody formBody=new FormBody.Builder()
//                        .add("username",username)
//                        .add("password",password)
//                        .build();
//                Request request=new Request.Builder().url(Information.URL+"login")
//                        .post(formBody).build();
//                Call call= OkHttpUtil.getOkHttpClient().newCall(request);
//                call.enqueue(new Callback() {
//                    @Override
//                    public void onFailure(@NonNull Call call, @NonNull IOException e) {
//                        tip.setText("登录失败");
//                    }
//
//                    @Override
//                    public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
//                        String responseJson = Objects.requireNonNull(response.body()).string();
//                        Gson gson= GsonUtil.getGsonBuilder().create();
//                        Map<String,String> ansMap=gson.fromJson(responseJson,Map.class);
//                        if(ansMap.get("state")==null){
//                            MainActivity.userid =ansMap.get("userid");
//                            MainActivity.username=ansMap.get("username");
//                            Intent intent=new Intent(LoginActivity.this,GameMainActivity.class);
//                            startActivity(intent);
//                            return;
//                        }
//                        if(Objects.requireNonNull(ansMap.get("state")).equals("未找到用户")){
//                            tip.setText("未找到用户");
//                        }else if(Objects.requireNonNull(ansMap.get("state")).equals("密码错误")){
//                            tip.setText("密码错误");
//                        }
//
//                    }
//                });
//
//
//
//            }
//        });
    }
}
/*
   String request_info = username + "," + password + ",";
                HttpUtil.sendHttpRequest("login.jsp", request_info, new HttpCallbackListener() {
                    @Override
                    public void onFinish(String response) {
                        String [] userinfo_buffer = response.split(",");
                        if(userinfo_buffer[0].equals("success")) //登录成功
                        {
                         ;
                        }
                        else{


                        }
                    }

                    @Override
                    public void onError(Exception e) {
                        login_tip.setText("登录失败");
                    }
                });
 */